var express = require('express');
var router = express.Router();

const { ResultSuccess, ResultError } = require('../utils/result')
const { login } = require('../controller/user')

// 登录
router.post('/login', function(req, res, next) {
  const result = login(req.body)
  result.then(data => {
    if (data.username) {
      // 设置 session
      req.session.username = data.username
      req.session.realname = data.realname
      res.json(new ResultSuccess('登录成功'))
    } else {
      res.json(new ResultError('登录失败'))
    }
  })
});

module.exports = router;
